package com.google.mr.okhttptest.adapter;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.mr.okhttptest.R;
import com.google.mr.okhttptest.bean.ZhihuBean;
import com.google.mr.okhttptest.utils.TimeUtil;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/11/27.
 */

public class CommonRecyclerViewAdapter extends RecyclerView.Adapter {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_DATE = 1;
    private static final int TYPYE_STORY = 2;

    private final Context mContext;
    private List<ZhihuBean.StoriesBean> mStories;
    private ZhihuBean.StoriesBean stories;
    private List<ZhihuBean.TopStoriesBean> mTopStoris;

    public CommonRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(ZhihuBean bean) {
        this.mStories = getRecyclerViewStorylist(bean);
        this.mTopStoris = bean.getTop_stories();
    }

    /**
     * 加载更多数据
     * @param response
     */
    public void addStories(ZhihuBean response) {
        int lastPosition = mStories.size() + 1;
        mStories.addAll(getRecyclerViewStorylist(response));
        notifyItemInserted(lastPosition);
    }

    /**
     * 为内容条目添加日期
     *
     * @param bean
     * @return
     */
    private List<ZhihuBean.StoriesBean> getRecyclerViewStorylist(ZhihuBean bean) {
        List<ZhihuBean.StoriesBean> storiesList = bean.getStories();
        ZhihuBean.StoriesBean storiesBean = new ZhihuBean.StoriesBean();
        storiesBean.setStoryDateItem(true);
        storiesBean.setTitle(TimeUtil.formatData(bean.getDate()));
        storiesList.add(0, storiesBean);
        return storiesList;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else if (mStories.get(position - 1).isStoryDateItem()) {
            return TYPE_DATE;
        } else {
            return TYPYE_STORY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_HEADER:
                holder = new HeaderItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview_header, parent, false));
                break;
            case TYPE_DATE:
                holder = new DateItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview_date, parent, false));
                break;
            case TYPYE_STORY:
                holder = new StoryItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview_story, parent, false));
                break;
            default:
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (position > 0) {
            stories = mStories.get(position - 1);
        }

        int viewType = holder.getItemViewType();
        switch (viewType) {
            case TYPE_HEADER:
                ((HeaderItemViewHolder) holder).setData(mTopStoris);
                break;
            case TYPE_DATE:
                ((DateItemViewHolder) holder).setData(stories.getTitle());
                break;
            case TYPYE_STORY:
                ((StoryItemViewHolder) holder).setData(stories);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mStories != null ? (mStories.size() + 1) : 0;
    }

    /**
     * 头部滚动布局
     */
    class HeaderItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rvp_topstoris)
        RollPagerView rollPagerView;

        public HeaderItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(final List<ZhihuBean.TopStoriesBean> mTopStoris) {
            //设置适配
            rollPagerView.setAdapter(new HeaderAdapter(mTopStoris));
            rollPagerView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    topStoryItemClickListener.onStoryItemClick(mTopStoris.get(position));
                }
            });
        }
    }

    /**
     * 日期布局
     */
    class DateItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.date_recyclerview)
        TextView textView;

        public DateItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(String title) {
            textView.setText(title);
        }
    }

    /**
     * 内容布局
     */
    class StoryItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.title_story)
        TextView title_strory;
        @BindView(R.id.image_story)
        ImageView imageView;
        @BindView(R.id.linearlayout_story)
        LinearLayout linearLayout;

        public StoryItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(final ZhihuBean.StoriesBean stories) {
            title_strory.setText(stories.getTitle());
            List<String> images = stories.getImages();
            //如果数据中某些无图需要默认图片设置
            if (images != null) {
                for (int i = 0; i < images.size(); i++) {
                    Glide.with(imageView.getContext())
                            .load(images.get(i))
                            .into(imageView);
                }
            } else {
                imageView.setImageResource(R.drawable.icon_no_image);
            }
                imageView.setVisibility(View.VISIBLE);

            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    storyItemClickListener.onStoryItemClick(stories);
                }
            });
        }
    }


    //顶部点击事件设置回调
    private TopStoryItemClickListener topStoryItemClickListener;

    public interface TopStoryItemClickListener{
        void onStoryItemClick(ZhihuBean.TopStoriesBean topStoriesBean);
    }

    public void setTopStoryItemClickListener(TopStoryItemClickListener topStoryItemClickListener){
        this.topStoryItemClickListener=topStoryItemClickListener;
    }

    //内容条目点击事件设置回调
    private StoryItemClickListener storyItemClickListener;
    public interface StoryItemClickListener{
        void onStoryItemClick(ZhihuBean.StoriesBean StoriesBean);
    }

    public void setStoryItemClickListener(StoryItemClickListener storyItemClickListener){
        this.storyItemClickListener=storyItemClickListener;
    }
}
